#include <nds.h>
#include <stdio.h>

#include "backgrounds.h"
#include "draws.h"
#include "splash.h"

#include "tiles.h"

int main(void){

  int bgMain256x256;
  int bgMain128x128;
  int bgSub256x256;
  int bgSub128x128;
  int bgTiles16x12;

  videoSetMode(MODE_5_2D);
  videoSetModeSub(MODE_5_2D);

  vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
  vramSetBankB(VRAM_B_MAIN_BG_0x06020000);
  vramSetBankC(VRAM_C_SUB_BG_0x06200000);

  PrintConsole pcTop = *consoleInit(0, 1, BgType_Text4bpp, BgSize_T_256x256, 0, 1, true, true);
  PrintConsole pcBtm = *consoleInit(0, 1, BgType_Text4bpp, BgSize_T_256x256, 0, 1, false, true);

  bgMain256x256 = initBg(true, 3, BgType_Bmp16, BgSize_B16_256x256, 2, 0);
  rotate(bgMain256x256, 128, 128, 0, 1 << 8, 1 << 8, 128, 128, 3);

  bgMain128x128 = initBg(true, 2, BgType_Bmp16, BgSize_B16_128x128, 8, 0);
  rotate(bgMain128x128, 0, 0, 0, 1 << 8, 1 << 8, -(SCREEN_WIDTH / 2 - 68), -(SCREEN_HEIGHT / 2 - 40), 2);

  bgSub256x256 = initBg(false, 3, BgType_Bmp16, BgSize_B16_256x256, 2, 0);
  rotate(bgSub256x256, 128, 128, 0, 1 << 8, 1 << 8, 128, 128, 3);

  //splash(bgMain256x256, bgMain128x128, bgSub256x256, *    rotate);

  u16* backGrounds[3] = {(u16*)BG_BMP_RAM(0), (u16*)BG_BMP_RAM(8), (u16*)BG_BMP_RAM_SUB(2)};
  clearBackGrounds(backGrounds, 2);

  bgMain256x256 = 0;
  bgMain128x128 = 0;
  bgSub256x256 = 0;
  bgSub128x128 = 0;
  bgTiles16x12 = 0;

  bgTiles16x12 = initBg(true, 1, BgType_Text8bpp, BgSize_T_256x256, 0, 1);
  bgMain256x256 = initBg(true, 2, BgType_Bmp16, BgSize_B16_256x256, 8, 0);
  rotate(bgMain256x256, 0, 0, 0, 1 << 8, 1 << 8, 0, 0, 2);

  dmaCopy(tilesPal, BG_PALETTE, tilesPalLen);
  dmaCopy(tilesTiles, bgGetGfxPtr(bgTiles16x12), tilesTilesLen);
  dmaCopy(tilesTiles, bgGetGfxPtr(bgMain256x256), tilesTilesLen);

  int tilePos;
  int tile;
  tilePos = 0;
  /*while(tilePos < 32 * 24){
    tile = 1;
    while(tile != 0 && tile != 16) tile = rand() % 17;
	drawBg32x32Iso(&tilePos, tile);
  }*/
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 0, 0);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 16, 1);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 32, 2);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 48, 3);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 64, 4);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 80, 5);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 96, 6);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 112, 7);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 128, 8);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 96, 9);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 80, 10);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 64, 11);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 48, 12);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 64, 13);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 80, 14);
  drawBg32x32Iso(bgGetMapPtr(bgTiles16x12), &tilePos, 96, 15);

  int row;
  int col;
  for(row = 0; row < SCREEN_HEIGHT; row++)
    for(col = 0; col < SCREEN_WIDTH; col++)
      *(bgGetGfxPtr(bgMain256x256) + col + (row * SCREEN_WIDTH)) = RGB15(0, 0, 0) | BIT(15);

  tilePos = 0;
  drawBg32x32Iso(bgGetMapPtr(bgMain256x256), &tilePos, 0, 0);

  iprintf("Hola Mundo");

  //videoSetMode(MODE_0_3D);

  return 0;

}